//
//  PasswordValidationView.swift
//  SCA
//
//  Created by tjun on 2025/9/1.
//

import UIKit


// MARK: - 规则定义
class PasswordRule {
    let title: String
    var isValid: Bool = false
    let check: (String) -> Bool
    
    init(title: String, isValid: Bool = false, check: @escaping (String) -> Bool) {
        self.title = title
        self.isValid = isValid
        self.check = check
    }
}

// MARK: - 自定义规则项 View
class PasswordRuleView: UIView {
    private let iconView = UIImageView()
    private let titleLabel = UILabel()

    init(title: String) {
        super.init(frame: .zero)
        setupView(title: title)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupView(title: String) {
        iconView.image = UIImage(systemName: "checkmark.circle.fill")
        iconView.tintColor = Assets.Color.labeledTextFieldTip
        iconView.translatesAutoresizingMaskIntoConstraints = false
        iconView.contentMode = .scaleAspectFit

        NSLayoutConstraint.activate([
            iconView.widthAnchor.constraint(equalToConstant: 16),
            iconView.heightAnchor.constraint(equalToConstant: 16)
        ])

        titleLabel.text = title
        titleLabel.textColor = Assets.Color.labeledTextFieldTip
        titleLabel.font = UIFont.systemFont(ofSize: 12)
        titleLabel.translatesAutoresizingMaskIntoConstraints = false

        let stack = UIStackView(arrangedSubviews: [iconView, titleLabel])
        stack.axis = .horizontal
        stack.spacing = 6
        stack.alignment = .center
        stack.translatesAutoresizingMaskIntoConstraints = false

        addSubview(stack)
        NSLayoutConstraint.activate([
            stack.topAnchor.constraint(equalTo: topAnchor),
            stack.leadingAnchor.constraint(equalTo: leadingAnchor),
            stack.trailingAnchor.constraint(equalTo: trailingAnchor),
            stack.bottomAnchor.constraint(equalTo: bottomAnchor)
        ])
    }

    func update(isValid: Bool) {
        iconView.image = UIImage(systemName: "checkmark.circle.fill")
        iconView.tintColor = isValid ? Assets.Color.hex34C759 : Assets.Color.labeledTextFieldTip
        titleLabel.textColor = isValid ? Assets.Color.hex34C759 : Assets.Color.labeledTextFieldTip
    }
}

class PasswordValidationView: UIView {
    private var stackView: UIStackView!
    private var ruleViews: [PasswordRuleView] = []
    private var rules: [PasswordRule] = []

    init(rules: [PasswordRule]) {
        super.init(frame: .zero)
        self.rules = rules
        setupView()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupView() {
        stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 8
        stackView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(stackView)

        NSLayoutConstraint.activate([
            stackView.topAnchor.constraint(equalTo: topAnchor),
            stackView.leadingAnchor.constraint(equalTo: leadingAnchor),
            stackView.trailingAnchor.constraint(equalTo: trailingAnchor),
            stackView.bottomAnchor.constraint(equalTo: bottomAnchor)
        ])

        for rule in rules {
            let ruleView = PasswordRuleView(title: rule.title)
            stackView.addArrangedSubview(ruleView)
            ruleViews.append(ruleView)
        }
    }

    func validate(password: String) {
        for (index, rule) in rules.enumerated() {
            let isValid = rule.check(password)
            rule.isValid = isValid
            ruleViews[index].update(isValid: isValid)
        }
    }
}
